package com.xm.xmds1705083.service.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xm.xm.dto.ResultDto;
import com.xm.xm.dto.SearchDto;
import com.xm.xm.util.BeanUtils;
import com.xm.xm.util.DateUtils;
import com.xm.xm.util.ErrorCode;
import com.xm.xm.util.ValidateUtils;
import com.xm.xmds1705083.service.mapper.DuoduoAreaMapper;
import com.xm.xmds1705083.service.mapper.DuoduoSpaceExchangeMapper;
import com.xm.xmds1705083.service.model.DuoduoSpaceExchange;
import com.xm.xmds1705083.service.service.DuoduoSpaceExchangeService;
import com.xm.xmds1705083.service.util.DateUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 杭州新苗网络科技有限公司
 *
 * @author meihf
 * @date 2017/06/14
 * @description
 */
@Service("duoduoSpaceExchangeService")
public class DuoduoSpaceExchangeServiceImpl extends ServiceImpl<DuoduoSpaceExchangeMapper, DuoduoSpaceExchange> implements DuoduoSpaceExchangeService {

    @Autowired
    DuoduoSpaceExchangeMapper duoduoSpaceExchangeMapper;

    @Autowired
    DuoduoAreaMapper duoduoAreaMapper;

    @Override
    public ResultDto findExchange(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        JSONObject filed = searchDto.getFiled();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            if (!filed.has("phone") || !ValidateUtils.isMobileNO(filed.getLong("phone"))) {
                resultDto.setMsg(ErrorCode.E10011);
                resultDto.setCode(ErrorCode.C10011);
                return resultDto;
            } else {
                String startTime = filed.getString("startTime");
                String endTime = filed.getString("endTime");
                String exchangeStartTime = String.valueOf(filed.remove("exStartTime"));
                String exchangeEndTime = String.valueOf(filed.remove("exEndTime"));
                int startTimeSecond = DateUtils.getStringToInt_seconds(startTime, DateUtils.PATTERN_DATE_TIME);
                int endTimeSecond = DateUtils.getStringToInt_seconds(endTime, DateUtils.PATTERN_DATE_TIME);
                //互换条件
                int exchangeStartTimeSecond = DateUtils.getStringToInt_seconds(exchangeStartTime,DateUtils.PATTERN_DATE_TIME);
                int exchangeEndTimeSecond = DateUtils.getStringToInt_seconds(exchangeEndTime,DateUtils.PATTERN_DATE_TIME);
                //时间变成时间戳
                filed.put("startTime",startTimeSecond);
                filed.put("endTime",endTimeSecond);
                filed.put("exStartTime",exchangeStartTimeSecond);
                filed.put("exEndTime",exchangeEndTimeSecond);
                filed.put("userId",searchDto.getAppUserId());
                filed.put("createTime",searchDto.getTime());
                //放到数据库中
                DuoduoSpaceExchange duoduoSpaceExchange = (DuoduoSpaceExchange) JSONObject.toBean(filed,DuoduoSpaceExchange.class);
                int result = duoduoSpaceExchangeMapper.insert(duoduoSpaceExchange);
                if (ValidateUtils.isGreaterThan0(result)){
                    //保存成功后，就立即匹配时间范围以及地区匹配 记住这里是互相匹配
                    EntityWrapper entityWrapper = new EntityWrapper();
                    entityWrapper.where(" startTime<={0} and endTime>={1} and userId<>{2} and county={3} ",startTimeSecond,endTimeSecond,searchDto.getAppUserId(),filed.getInt("county")).orderBy("createTime",false);
                    RowBounds rowBounds = new RowBounds(searchDto.getPageNo(),searchDto.getPageSize());
                    List<DuoduoSpaceExchange> duoduoSpaceExchangeList = duoduoSpaceExchangeMapper.selectPage(rowBounds,entityWrapper);
                    if (duoduoSpaceExchangeList.size()==0){
                        resultDto.setCode(ErrorCode.C11001);
                        resultDto.setMsg(ErrorCode.E11001);
                        return resultDto;
                    }else {
                        JSONArray array = JSONArray.fromObject(duoduoSpaceExchangeList);
                        array.forEach((l)->{
                            ((JSONObject)l).put("area",duoduoAreaMapper.selectById(((JSONObject) l).getLong("area")).getArea());
                            ((JSONObject)l).put("province",duoduoAreaMapper.selectById(((JSONObject) l).getLong("province")).getArea());
                            ((JSONObject)l).put("county",duoduoAreaMapper.selectById(((JSONObject) l).getLong("county")).getArea());
                            ((JSONObject)l).put("startTime",DateUtil.timeStamp2Date(((JSONObject) l).getLong("startTime"),DateUtils.PATTERN_DATE_TIME));
                            ((JSONObject)l).put("endTime",DateUtil.timeStamp2Date(((JSONObject) l).getLong("endTime"),DateUtils.PATTERN_DATE_TIME));
                        });
                        resultDto.setData(array);
                        resultDto.setMsg("查询成功！");
                    }
                }else {
                    resultDto.setMsg(ErrorCode.E11004);
                    resultDto.setCode(ErrorCode.C11004);
                }
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

}

